package com.codename1.samples;


import com.codename1.components.SpanButton;
import com.codename1.components.SpanLabel;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.testing.AbstractTest;
import com.codename1.ui.Button;
import com.codename1.ui.CN;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.geom.Dimension;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.FlowLayout;
import com.codename1.ui.layouts.Layout;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class SpanLabelTest2980 {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Layout lay = new FlowLayout();
        //Layout lay = BoxLayout.y();
        Form hi = new Form("Gallery Test", lay);
        hi.add(new SpanLabel("Tap the following button to open the gallery. You should be able to select multiple images and videos."));
        
        Button btn = new Button("Run Test");
        SpanLabel files = new SpanLabel();
        SpanLabel result = new SpanLabel();
        btn.addActionListener(l -> {
            try {
                SpanLabelTests2980 test = new SpanLabelTests2980();
                test.runTest();
                result.setText("Test Passed");
            } catch (Throwable t) {
                result.setText("Failed: "+t.getMessage());
            }
            hi.revalidateWithAnimationSafety();
        });
        hi.addAll(btn, files, result);
        
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

    public class SpanLabelTests2980 extends AbstractTest {
        private Layout layout;

        
        

        @Override
        public boolean shouldExecuteOnEDT() {
            return true;
        }

        
        
        @Override
        public boolean runTest() throws Exception {
            if (layout == null) {
                Layout[] layouts = new Layout[]{
                    new FlowLayout(),
                    BoxLayout.x(),
                    BoxLayout.y()
                };
                for (Layout l : layouts) {
                    layout = l;
                    runTest();
                }
                return true;
            }
            System.out.println("Laying out SpanLabel with layout "+layout);
            Label label = new Label("Tap the following");
            
            
            Container cnt = new Container(layout) {
                @Override
                protected Dimension calcPreferredSize() {
                    return new Dimension(label.getPreferredW(), CN.convertToPixels(1000));
                }

                @Override
                public int getWidth() {
                    return label.getPreferredW();
                }

                @Override
                public int getHeight() {
                    return CN.convertToPixels(1000);
                }
                
                
                
                
                
                
            };
            cnt.setScrollableX(false);
            cnt.setScrollableY(false);
            cnt.setWidth(label.getPreferredW());
            cnt.setHeight(CN.convertToPixels(1000));
            SpanLabel sl = new SpanLabel("Tap the following button to open the gallery. You should be able to select multiple images and videos.");
           
            sl.setName("TheSpanLabel");
            cnt.add(sl);
            cnt.add(new Button("Click Me"));
            cnt.setShouldCalcPreferredSize(true);
            cnt.layoutContainer();
            assertTrue(sl.getHeight() > label.getPreferredH() * 2, "Span Label height is too low for layout "+layout+": was "+sl.getHeight()+" but should be at least "+(label.getPreferredH() * 2));
            
            SpanButton sb = new SpanButton("Tap the following button to open the gallery. You should be able to select multiple images and videos.");
           
            sb.setName("TheSpanButton");
            cnt.removeAll();
            cnt.add(sb);
            cnt.add(new Button("Click Me"));
            cnt.setShouldCalcPreferredSize(true);
            cnt.layoutContainer();
            assertTrue(sb.getHeight() > label.getPreferredH() * 2, "Span button height is too low for layout "+layout+": was "+sb.getHeight()+" but should be at least "+(label.getPreferredH() * 2));
            
            
            return true;
        }
        
    }
    
}
